package entity

import (
	"fmt"

	"gitee.com/night-tc/gobige/common"
	"gitee.com/night-tc/gobige/global"
	"gitee.com/night-tc/gobige/logger"
	"gitee.com/night-tc/gobige/msgdef"
	"gitee.com/night-tc/gobige/msgdef/protomsg"
)

/*
服务器上承载属性的对象

*/

var (
	_typeMgr *TypeMgr
)

func init() {
	_typeMgr = newTypeMgr()
}

// 获取对象类型管理器
func GetTypeMgr() *TypeMgr {
	return _typeMgr
}

func argToRespInfo(val interface{}) *protomsg.RespParam {
	param := &protomsg.RespParam{}
	switch v := val.(type) {
	case int32:
		param.ParamValue = &protomsg.RespParam_Int32Value{
			Int32Value: v,
		}
	case uint32:
		param.ParamValue = &protomsg.RespParam_Uint32Value{
			Uint32Value: v,
		}
	case int64:
		param.ParamValue = &protomsg.RespParam_Int64Value{
			Int64Value: v,
		}
	case int:
		param.ParamValue = &protomsg.RespParam_Int64Value{
			Int64Value: int64(v),
		}
	case uint64:
		param.ParamValue = &protomsg.RespParam_Uint64Value{
			Uint64Value: v,
		}
	case float32:
		param.ParamValue = &protomsg.RespParam_FloatValue{
			FloatValue: v,
		}
	case float64:
		param.ParamValue = &protomsg.RespParam_DoubleValue{
			DoubleValue: v,
		}
	case string:
		param.ParamValue = &protomsg.RespParam_StringValue{
			StringValue: v,
		}
	default:
		param.ParamValue = &protomsg.RespParam_StringValue{
			StringValue: fmt.Sprint(val),
		}
		// log.Error("RetMsg error !", val)
		return nil
	}

	return param
}

/*
回复消息
user 回复对象
retmsgid 回复消息号
err 错误信息
*/
func RetMsg(user IPost, msg msgdef.IMsg, err error) {
	if user == nil {
		return
	}
	var msgid uint16
	if msg != nil {
		msgid, _ = msgdef.GetTypeMgr().GetMsgIDByName(msg.Name())
	}

	RespMsg := &protomsg.MsgRet{
		RetCode:  0,
		RetMsgID: uint32(msgid),
		Params:   []*protomsg.RespParam{},
	}

	if msgerr, ok := err.(*common.Error); ok {
		RespMsg.RetCode = msgerr.ErrCode
		for _, v := range msgerr.Param {
			param := argToRespInfo(v)
			if param == nil {
				logger.Error("RetMsg error !", v)
				return
			}
			RespMsg.Params = append(RespMsg.Params, param)
		}
	} else if err != nil {
		RespMsg.RetCode = -1
		logger.Error("RetMsg error.", err)
	}
	user.DelayPost(global.ServerTypeClient, RespMsg)
}

// 让客户端返回登录消息
func ReturnLLoginNtf(user IPost, err error) {
	if user == nil {
		return
	}

	RespMsg := &protomsg.ReturnLoginNtf{
		RetCode: 0,
		Params:  []*protomsg.RespParam{},
	}

	if msgerr, ok := err.(*common.Error); ok {
		RespMsg.RetCode = msgerr.ErrCode
		for _, v := range msgerr.Param {
			param := argToRespInfo(v)
			if param == nil {
				logger.Error("RetMsg error !", v)
				return
			}
			RespMsg.Params = append(RespMsg.Params, param)
		}
	} else if err != nil {
		RespMsg.RetCode = -1
		logger.Error("RetMsg error.", err)
	}
	_ = user.Post(global.ServerTypeClient, RespMsg)
}

// 打包成发给客户端的消息
func PackEntityMsgRet(nttmd IBase, msg msgdef.IMsg) (result *protomsg.EntityMsgRet) {
	result = new(protomsg.EntityMsgRet)
	result.EntityID = nttmd.GetEntityID()
	msgid, _ := msgdef.GetTypeMgr().GetMsgIDByName(msg.Name())
	result.MsgID = uint32(msgid)
	result.MsgContent, _ = msg.Marshal()
	return
}
